package cn.shutdown.progressbartest;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;

public class MainActivity extends Activity {

	private int[] data = new int[100];
	int hasData = 0;
	int status = 0;
	ProgressBar bar1, bar2;
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message message) {
			if (message.what == 0x111) {
				bar1.setProgress(status);
				bar2.setProgress(status);
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bar1 = (ProgressBar) findViewById(R.id.bar);
		bar2 = (ProgressBar) findViewById(R.id.bar2);
		new Thread() {
			public void run() {
				while (status < 100) {
					status = doWork();
					System.out.println(status);
					handler.sendEmptyMessage(0x111);
				}
			}
		}.start();

	}

	public int doWork() {
		data[hasData++] = (int) (Math.random() * 100);
		try {
			Thread.sleep(100);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
		return hasData;

	}

}
